package com.hyy.language;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LanguageUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @ClassName: com.gapsk.ai_chinese_test.ui.widget
 * @Description:
 * @Author: hyy
 * @Date: 2020/8/21
 * @Time: 10:13 AM
 */
public class SelectLanguageDialog extends Dialog {

    private RecyclerView recyclerView;
    private TextView tv_title;
    private Button btConfirm;
    private List<LanguageDataBean> languages;
    private LanguageAdapter languageAdapter;

    public static final String EN_CODE = "en";
    public static final String ZH_CODE = "zh-hans";
    public static final String TW_CODE = "zh-hant";
    public static final String TH_CODE = "th";
    public static final String VI_CODE = "vi";
    public static final String ID_CODE = "id";

    public SelectLanguageDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_language);
        setCanceledOnTouchOutside(false);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        languages = new ArrayList<>();

        for (int i = 0; i < 6; i++) {
            LanguageDataBean languageDataBean = null;
            if (i == 0) {
                languageDataBean = new LanguageDataBean();
                languageDataBean.setCode(EN_CODE);
                languageDataBean.setTitle("English");
            } else if (i == 2) {
                languageDataBean = new LanguageDataBean();
                languageDataBean.setCode(TH_CODE);
                languageDataBean.setTitle("ไทย");
            } else if (i == 1) {
//                languageDataBean = new LanguageDataBean();
//                languageDataBean.setCode(VI_CODE);
//                languageDataBean.setTitle("Tiếng Việt");
            } else if (i == 3) {
                languageDataBean = new LanguageDataBean();
                languageDataBean.setCode(ID_CODE);
                languageDataBean.setTitle("bahasa Indonesia");
            } else if (i == 4) {
                languageDataBean = new LanguageDataBean();
                languageDataBean.setCode(ZH_CODE);
                languageDataBean.setTitle("中文简体");
            } else if (i == 5) {
                languageDataBean = new LanguageDataBean();
                languageDataBean.setCode(TW_CODE);
                languageDataBean.setTitle("中文繁體");
            }
            if (languageDataBean != null) {
                languages.add(languageDataBean);
            }

        }
        initView();
    }


    private void initView() {
        btConfirm = findViewById(R.id.bt_confirm);
        tv_title = findViewById(R.id.tv_title_name);
        btConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LanguageDataBean languageDataBean = languages.get(languageAdapter.selectPostion);
                String selectLanguage = languageDataBean.getCode();
                String selectLanguageName = languageDataBean.getTitle();

                CommonUtils.setCurLanguageName(selectLanguageName);
                CommonUtils.setCurLanguage(selectLanguage);
                Locale locale = CommonUtils.getLanguageLocal(selectLanguage);
                LanguageUtils.applyLanguage(locale);
                onclinckCallBack.onclinckCallBack(languageDataBean);
                dismiss();
            }
        });

        recyclerView = findViewById(R.id.recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        languageAdapter = new LanguageAdapter(languages);
        recyclerView.setAdapter(languageAdapter);

    }

    class LanguageAdapter extends BaseQuickAdapter<LanguageDataBean, BaseViewHolder> {

        private int selectPostion;

        public LanguageAdapter(List<LanguageDataBean> data) {
            super(R.layout.layout_select_language_item, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, LanguageDataBean languageDataBean) {
            RelativeLayout rootView = baseViewHolder.getView(R.id.root_view);
            ImageView img_select = baseViewHolder.getView(R.id.img_select);
            TextView tv_language_name = baseViewHolder.getView(R.id.tv_language_name);
            tv_language_name.setText(languageDataBean.getTitle());


            if (selectPostion == baseViewHolder.getAdapterPosition()) {
                rootView.setBackgroundResource(R.color.color_f4f7f9);
                img_select.setVisibility(View.VISIBLE);
            } else {
                rootView.setBackgroundResource(R.color.white);
                img_select.setVisibility(View.GONE);
            }

            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (selectPostion != baseViewHolder.getAdapterPosition()) {

                        int oldSelectPostion = selectPostion;
                        selectPostion = baseViewHolder.getAdapterPosition();
                        notifyItemChanged(oldSelectPostion);
                        notifyItemChanged(selectPostion);
                    }

                }
            });
        }
    }

    private OnclinckCallBack onclinckCallBack;

    public void setOnclinckCallBack(OnclinckCallBack onclinckCallBack) {
        this.onclinckCallBack = onclinckCallBack;
    }

    public interface OnclinckCallBack {
        void onclinckCallBack(LanguageDataBean selectLanguage);
    }

    public void setLanguageDatas(List<LanguageDataBean> languageDatas) {
        languages.clear();
        languages.addAll(languageDatas);
        languageAdapter.notifyDataSetChanged();
    }

    public void setData(String title, String configm) {
//        tv_title.setText(title);
//        btConfirm.setText(configm);
        //TODO log 调式
        Log.i("SelectLanguageDialog", tv_title + "setData===>" + btConfirm);
        Log.i("SelectLanguageDialog", title + "setData===>" + configm);
    }

}
